package com.wxp.uniplugin_amap.service;

import com.alibaba.fastjson.JSONObject;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;

public class AMapLocationListenerImpl implements AMapLocationListener {

    private JSONObject jsonObject;

    public AMapLocationListenerImpl() {

    }

    public AMapLocationListenerImpl(JSONObject jsonObject) {
        this.jsonObject = jsonObject;
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation == null) return;
        if (aMapLocation.getErrorCode() == 0) {
            jsonObject.put("code", aMapLocation.getErrorCode());
            jsonObject.put("type", aMapLocation.getLocationType());
            jsonObject.put("lng", aMapLocation.getLongitude());
            jsonObject.put("lat", aMapLocation.getLatitude());
            jsonObject.put("speed", aMapLocation.getSpeed());
            jsonObject.put("country", aMapLocation.getCountry());
            jsonObject.put("province", aMapLocation.getProvince());
            jsonObject.put("city", aMapLocation.getCity());
            jsonObject.put("district", aMapLocation.getDistrict());
            jsonObject.put("address", aMapLocation.getAddress());
        } else {
            jsonObject.put("code", aMapLocation.getErrorCode());
            jsonObject.put("message", "定位错误");
        }
    }
}
